using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;

namespace OpenFlashChartLib
{
    public class NotifyCollection<TItem> : CollectionBase
    {
        internal event ChangedEventHandler<TItem> Change;

        public TItem this[int index]
        {
            get
            {
                return (TItem)List[index];
            }
            set
            {
                List[index] = value;
            }
        }

        public int Add(TItem value)
        {
            return List.Add(value);
        }

        public void Remove(TItem value)
        {
            List.Remove(value);
        }

        public void Insert(int index, TItem item)
        {
            List.Insert(index, item);
        }

        protected override void OnClear()
        {
            base.OnClear();
            foreach (TItem c in this)
            {
                OnChange(new ChangedEventArgs<TItem>(ChangedAction.Remove, c));
            }
        }

        protected override void OnRemove(int index, object value)
        {
            base.OnRemove(index, value);
            OnChange(new ChangedEventArgs<TItem>(ChangedAction.Remove,
                (TItem)value, index));
        }

        protected override void OnSet(int index, object oldValue, object newValue)
        {
            base.OnSet(index, oldValue, newValue);
            OnChange(new ChangedEventArgs<TItem>(ChangedAction.Set,
                (TItem)newValue));
        }

        protected override void OnInsert(int index, object value)
        {
            base.OnInsert(index, value);
            OnChange(new ChangedEventArgs<TItem>(ChangedAction.Insert,
                (TItem)value, index));
        }

        protected override void OnValidate(object value)
        {
            if (!(value is TItem))
            {
                throw
                    new ArgumentException("DXControlCollection operates with DXControl objects only");
            }
        }

        protected void OnChange(ChangedEventArgs<TItem> e)
        {
            if (Change != null)
            {
                Change(this, e);
            }
        }
    }

}
